package com.mychart.config;

import com.mychart.constant.WsConstant;
import com.mychart.data.LoginUserSussionRepository;
import com.mychart.handler.ws.UserHandlerShakeHandler;
import com.mychart.handler.ws.UserWsHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.List;
import java.util.Map;

@Configuration
@EnableWebSocket
public class WsConfig implements WebSocketConfigurer {

    @Autowired
    UserHandlerShakeHandler userHandlerShakeHandler;
    @Autowired
    LoginUserSussionRepository repository;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new UserWsHandler(repository,mongoTemplate),"loginWs")
                .addInterceptors(userHandlerShakeHandler)
                                 .setAllowedOriginPatterns("*");
    }
}
